package com.suhuamo.transtionals.controller;

import com.suhuamo.transtionals.mapper.UserMapper;
import com.suhuamo.transtionals.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * @author yuanchuncheng
 * @slogan 耐心等，太阳总会升起来的。
 * @date 2024-12-21
 * @description
 */
@RestController
@RequestMapping("/redis")
public class RedisController {
    // 自带 RedisTemplate
    @Autowired
    @Qualifier("stringRedisTemplate")
    private RedisTemplate<String,String> stringRedisTemplate;

    // 我们自定义的 RedisTemplate ，即带有事务的 RedisTemplate
    @Autowired
    @Qualifier("transactionRedisTemplate")
    private RedisTemplate<String,String> transactionRedisTemplate;

    @Autowired
    UserMapper userMapper;

    /**
     * 正常写入
     * @param
     * @return void
     * @version 1.0
     * @author yuanchuncheng
     */
    @GetMapping("/no-1")
    public void no1(){
        stringRedisTemplate.opsForValue().set("test", "no-1");
    }

    /**
     * 不会回滚，正常写入
     * @param
     * @return void
     * @version 1.0
     * @author yuanchuncheng
     */
    @GetMapping("/no-2")
    public void no2(){
        stringRedisTemplate.opsForValue().set("test", "no-2");
        int i = 1 / 0;
    }

    /**
     * 不会回滚，正常写入
     * @param
     * @return void
     * @version 1.0
     * @author yuanchuncheng
     */
    @GetMapping("/no-3")
    @Transactional
    public void no3(){
        stringRedisTemplate.opsForValue().set("test", "no-3");
        int i = 1 / 0;
    }

    /**
     * 正常写入
     * @param
     * @return void
     * @version 1.0
     * @author yuanchuncheng
     */
    @GetMapping("/yes-1")
    @Transactional
    public void yes1(){
        transactionRedisTemplate.opsForValue().set("test", "yes-1");
    }

    /**
     * 执行回滚操作，写入失败
     * @param
     * @return void
     * @version 1.0
     * @author yuanchuncheng
     */
    @GetMapping("/yes-2")
    @Transactional
    public void yes2(){
        transactionRedisTemplate.opsForValue().set("test", "yes-2");
        int i = 1 / 0;
    }

    /**
     * 不会回滚，正常写入——因为没带上  @Transactional 注解，即未开启事务
     * @param
     * @return void
     * @version 1.0
     * @author yuanchuncheng
     */
    @GetMapping("/yes-3")
    public void yes3(){
        transactionRedisTemplate.opsForValue().set("test", "yes-3");
        int i = 1 / 0;
    }

    /**
     * 正常写入【带有事务的RedisTemplate就算不加事务注解也能正常运行】
     * @param
     * @return void
     * @version 1.0
     * @author yuanchuncheng
     */
    @GetMapping("/yes-4")
    public void yes4(){
        transactionRedisTemplate.opsForValue().set("test", "yes-4");
    }

    @GetMapping("/yes-5")
    //@Transactional
    public long yes5() throws InterruptedException {
        transactionRedisTemplate.opsForValue().set("test", String.valueOf(System.currentTimeMillis()));
        System.out.println("System.currentTimeMillis() = " + System.currentTimeMillis());
        //User user = userMapper.selectById(1);
        //Thread.sleep(2_000);
        transactionRedisTemplate.opsForValue().get("test");
        return System.currentTimeMillis();
    }

    @GetMapping("/yes-6")
    @Transactional
    public void yes6(){
        transactionRedisTemplate.opsForValue().set("test", "yes-6");
        transactionRedisTemplate.opsForValue().get("test");
        User user = userMapper.selectById(1);
    }

    @GetMapping("/yes-7")
    @Transactional
    public String yes7(){
        return transactionRedisTemplate.opsForValue().get("test");
    }
}
